package mapReduce.util;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.SequenceFile;
import org.apache.hadoop.io.Text;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;

public class GenerateData {

    static Random random = new Random();

    /**
     * 生成输入格式化的序列化文件数据
     */
    public static void genInputFormatSeqData() throws Exception {

        String[] nameArray = {"tom","jerry","波哥","波老板","玩游戏"};
        int[] ageArray = {20,21,22,40,60};

        Configuration conf = new Configuration();
        conf.set("fs.defaultFS","file:///");
        FileSystem fs = FileSystem.get(conf);

        //创建序列化文件的输出流
        SequenceFile.Writer writer = SequenceFile.createWriter(
            fs,
            conf,
            new Path("file:///D:/data/data_hadoop/InputFormat/input_seq/nameAndAge.seq"),
            Text.class,
            IntWritable.class
        );

        Text key = new Text();
        IntWritable value = new IntWritable();

        for (int i = 0; i < nameArray.length; i++) {
            key.set(nameArray[i]);
            value.set(ageArray[i]);

            writer.append(key,value);
            writer.hflush();
        }

        writer.close();
    }

    /**
     * 生成预合并的测试数据
     * 格式为
     *      2025-01 20
     *      ......
     */
    public static void genYearMonthTempData() throws IOException {

        String year = "2025-";

        FileWriter fw = new FileWriter("D:/data/data_hadoop/temperatureMonthCount/input/yearMonthTemp3.txt");

        for (int i = 0; i < 1000 ; i++) {
            int month = random.nextInt(12)+1;
            String yearMonth = year+String.format("%02d",month);
            int temperature = random.nextInt(61)-30;

            String line = yearMonth+"\t"+temperature+"\n";

            fw.write(line);
            fw.flush();
        }
        fw.close();
    }

    /**
     * 生成自定义序列化类型的自定义分组规则的测试数据
     * 数据格式     group-01	tom	20	男
     * 	           group-01	jerry	30	女
     */
    public static void genGroupGenderData() throws IOException {

        String[] genders = {"男","女"};

        FileWriter fw = new FileWriter("D:/data/data_hadoop/genderGroup/input/groupMember.txt");

        for (int i = 0; i <1000 ; i++) {

            String groupName = "group-" + String.format("%02d",random.nextInt(20)+1);

            char[] chars = new char[5];
            for (int j = 0; j <5 ; j++) {
                chars[j] = (char) (random.nextInt(26)+97);
            }
            String userName = new String(chars);

            int age = random.nextInt(6)+20;

            String gender = genders[random.nextInt(genders.length)];

            String line = groupName+"\t"+userName+"\t"+age+"\t"+gender+"\n";

            fw.write(line);

            fw.flush();

        }
        fw.close();
    }

    /**
     * 生成全排序业务的测试数据
     * 数据格式
     *      1900    20
     */
    public static void genYearTempData() throws IOException {

        FileWriter fw = new FileWriter("D:/data/data_hadoop/temperatureCount/input/yearTemp.txt");

        //1900 - 2000
        for (int i = 0; i < 100000; i++) {
            int year = random.nextInt(101)+1900;
            int temp = random.nextInt(61)-30;

            fw.write(year+"\t"+temp+"\n");

            fw.flush();
        }

        //2001 - 2020
        for (int i = 0; i < 900000; i++) {
            int year = random.nextInt(20)+2001;
            int temp = random.nextInt(61)-30;

            fw.write(year+"\t"+temp+"\n");

            fw.flush();
        }

        fw.close();

    }

    public static void genYearTempSeqData() throws IOException {

        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(conf);

        SequenceFile.Writer writer = SequenceFile.createWriter(
                fs,
                conf,
                new Path("D:/data/data_hadoop/temperatureCount/input/yearTemp.seq"),
                Text.class,
                Text.class
        );

        Text key = new Text();
        Text value = new Text();

        for (int i = 0; i <100000 ; i++) {
            int year = random.nextInt(101)+1900;
            int temp = random.nextInt(61)-30;

            key.set(year+"");
            value.set(temp+"");

            writer.append(key,value);
            writer.hflush();
        }

        for (int i = 0; i <900000 ; i++) {
            int year = random.nextInt(20)+2001;
            int temp = random.nextInt(61)-30;

            key.set(year+"");
            value.set(temp+"");

            writer.append(key,value);
            writer.hflush();
        }

        writer.close();
    }

    public static void main(String[] args) throws Exception {

//        genInputFormatSeqData();

//        genYearMonthTempData();
//        genGroupGenderData();
//        genYearTempData();
        genYearTempSeqData();
    }

}
